package com.zyz.common.exception;

import com.zyz.base.enums.ResponseCodeEnum;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wangjinliang
 */
public class ErrorCodeException extends RuntimeException {
    @Setter @Getter
    private int code;

    public ErrorCodeException(ResponseCodeEnum errorCode) {
        super(errorCode.getCode() + ":" + errorCode.getMessage());
        this.code = errorCode.getCode();
    }

    public ErrorCodeException(ResponseCodeEnum errorCode, String message) {
        super(message);
        this.code = errorCode.getCode();
    }

    @Override
    public String toString() {
        String clazzName = getClass().getName();
        String message = getLocalizedMessage();
        return (StringUtils.isNotBlank(message)) ? (clazzName + ": " + message) : (clazzName + ": " + code);
    }
}
